package online.foxnull2.foxmall.seckill.scheduler;

import lombok.extern.slf4j.Slf4j;
import online.foxnull2.foxmall.seckill.service.SeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class SeckillSkuScheduler {

    private static final String UPLOAD_LOCK = "seckill:upload:lock";

    private final SeckillService seckillService;

    @Autowired
    public SeckillSkuScheduler(SeckillService seckillService) {
        this.seckillService = seckillService;
    }

    @Async
    @Scheduled(cron = "0 0 3 * * ?")
    public void uploadSeckillSku() {
        log.debug("upload seckill sku START");
        seckillService.uploadSeckillSku();
        log.debug("upload seckill sku END");
    }

//    @Async
//    @Scheduled(cron = "0 * * * * ?")
//    public void test() {
//        log.debug("upload seckill sku START");
//        RLock lock = redisson.getLock(UPLOAD_LOCK);
//        lock.lock(10, TimeUnit.SECONDS);
//        try {
//            seckillService.uploadSeckillSku();
//        } finally {
//            lock.unlock();
//        }
//        log.debug("upload seckill sku END");
//    }
}
